<template>
  <div>
    <van-nav-bar
      :title="'购物车' + '(' + store.state.cartCount + ')'"
      left-arrow
      fixed
      placeholder
      @click-left="goBack"
    >
      <template #right>
        <van-icon name="search" size="18" />
      </template>
    </van-nav-bar>
    .
    <van-checkbox-group
      v-model="checkedItem"
      @change="checkedItemChange"
      class="cart-box"
    >
      <van-swipe-cell v-for="item in cartList" :key="item.id">
        <div class="cart-item">
          <van-checkbox :name="item.id"></van-checkbox>
          <div class="cart-img">
            <img v-lazy="item.goods.cover_url" alt="" />
          </div>
          <div class="cart-info">
            <div class="cart-info-dh">
              <span>{{ item.goods.title }}</span>
              <span>{{ item.goods.stock }}</span>
            </div>
            <div class="cart-info-db">
              <span class="cart-info-price"
                ><small>￥</small>{{ item.goods.price.toFixed(2) }}</span
              >
              <van-stepper v-model="item.num" />
            </div>
          </div>
        </div>
        <template #right>
          <van-button
            style="height: 100%"
            square
            icon="delete-o"
            type="danger"
            class="delete-button"
            @click="deleteCartClick(item.id)"
          />
        </template>
      </van-swipe-cell>
    </van-checkbox-group>
    <div class="bg-box" v-if="store.state.cartCount === 0">
      <img src="../../assets/images/empty-car.png" alt="" />
      <p>购物车空空如也</p>
      <van-button round type="primary" @click="router.push('/home')"
        >前往购物</van-button
      >
    </div>
    <van-submit-bar
      :price="cartTotal * 100"
      button-text="结算"
      @submit="onSubmit"
    >
      <van-checkbox v-model="checkedAll" @click="checkedAllChange"
        >全选</van-checkbox
      >
    </van-submit-bar>
  </div>
</template>
<script>
import { useRouter } from 'vue-router'
import { computed, onMounted, reactive, toRefs } from 'vue'
import { getCartList, checkedCart, deleteCart } from '../../network/cart'
import { useStore } from 'vuex'
import { Toast } from 'vant'
export default {
  setup () {
    const router = useRouter()
    const store = useStore()
    const state = reactive({
      cartList: [],
      checkedAll: true,
      checkedItem: []
    })
    const init = () => {
      Toast.loading({ message: '加载中...', forbidClick: true })
      getCartList({ include: 'goods' }).then(res => {
        console.log(res)
        state.cartList = res.data
        state.checkedItem = state.cartList.filter(item => item.is_checked === 1).map(item => item.id)
        Toast.clear()
      })
    }
    onMounted(() => {
      init()
    })
    const checkedItemChange = (reslut) => {
      if (reslut.length === state.cartList.length) {
        state.checkedAll = true
      } else {
        state.checkedAll = false
      }
      state.checkedItem = reslut
      checkedCart({ cart_ids: reslut })
    }
    const checkedAllChange = () => {
      if (state.checkedAll) {
        console.log(state.checkedAll)
        state.checkedItem = state.cartList.map(item => item.id)
        state.checkedAll = true
      } else {
        state.checkedItem = []
        state.checkedAll = false
      }
    }
    const onSubmit = () => {
      if (state.checkedItem.length === 0) {
        Toast.fail('请选择商品进行结算')
      } else {
        router.push('/createorder')
      }
    }
    const cartTotal = computed(() => {
      let sun = 0
      state.cartList.filter(item => state.checkedItem.includes(item.id)).forEach(item => {
        sun += item.num * item.goods.price
      })
      return sun
    })
    const deleteCartClick = (cartId) => {
      deleteCart(cartId).then(res => {
        init()
        store.dispatch('storeAddcart')
      })
    }
    const goBack = () => {
      router.go(-1)
    }

    return {
      ...toRefs(state),
      goBack,
      cartTotal,
      onSubmit,
      checkedAllChange,
      checkedItemChange,
      store,
      deleteCart,
      deleteCartClick,
      router
    }
  }
}
</script>
<style scoped lang="less">
.van-submit-bar {
  bottom: 50px;
}
.cart-box {
  margin: 15px 0 50px;
}
.cart-item {
  display: flex;
  padding: 0 10px;
  .cart-img {
    width: 100px;
    height: 100px;
    img {
      width: 100%;
      height: 100%;
      padding: 10px;
    }
  }
  .cart-info {
    flex: 1;
    display: flex;
    padding: 15px;
    flex-direction: column;
    justify-content: space-between;
    .cart-info-dh,
    .cart-info-db {
      display: flex;
      justify-content: space-between;
    }
    .cart-info-price {
      color: red;
    }
  }
}
.bg-box {
  margin-top: 100px;
  img {
    width: 50%;
  }
  p {
    margin: 15px 0;
  }
  .van-button--round {
    width: 50%;
  }
}
</style>
